package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.Customer;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.CustomerMapper;
import com.kgc.basicprovider.service.CustomerService;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class CustomerServiceImpl implements CustomerService {

    private Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public ResponseMsg selectAllCustomer() {
        logger.info("CustomerServiceImpl selectAllCustomer is start...");
        ResponseMsg msg = new ResponseMsg();
        List<Customer> customerList = customerMapper.selectAllCustomer();
        if (customerList == null && customerList.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("为查询到任何数据！");
            return msg;
        }
        msg.setCode("200");
        msg.setData(customerList);
        return msg;
    }

    @Override
    public ResponseMsg selectCustomerByCode(String code) {
        logger.info("CustomerServiceImpl selectCustomerByCode code:{}", code);
        ResponseMsg msg = new ResponseMsg();
        Customer customer = customerMapper.selectCustomerByCode(code);
        if (customer != null) {
            msg.setCode("301");
            msg.setMsg("客户编码不能重复！");
            return msg;
        }
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMsg selectCustomerByPage(Map<String, Object> map) {
        logger.info("CustomerServiceImpl selectCustomerByPage params,{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        PageInfo<Customer> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize(), "create_date desc");
        List<Customer> customerList = customerMapper.selectCustomerByPage(map);
        pageInfo.setList(customerList);
        pageInfo.setTotal((int) page.getTotal());
        responseMsg.setData(pageInfo);
        logger.debug("CustomerServiceImpl selectCustomerByPage result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCustomer(Customer customer) {
        logger.info("CustomerServiceImpl insertCustomer params,{}", customer);
        ResponseMsg responseMsg = new ResponseMsg();
        customer.setCreateDate(new Date());
        customer.setUpdateDate(new Date());

//        customer.setCode();

        int result = customerMapper.insertCustomer(customer);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("CustomerServiceImpl insertCustomer result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateCustomer(Customer customer) {
        logger.info("CustomerServiceImpl updateCustomer params,{}", customer);
        ResponseMsg responseMsg = new ResponseMsg();
        int result = customerMapper.updateCustomer(customer);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("CustomerServiceImpl updateCustomer result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg deleteCustomer(int id) {
        logger.info("CustomerServiceImpl deleteCustomer params,{}", id);
        ResponseMsg responseMsg = new ResponseMsg();
        int result = customerMapper.deleteCustomer(id);
        if (result > 0) {
            responseMsg.setCode("200");
        } else {
            responseMsg.setCode("201");
        }
        logger.debug("CustomerServiceImpl deleteCustomer result:{}", responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg getAllCustomerByData() {
        logger.info("CustomerServiceImpl getAllCustomerByData is start...");
        ResponseMsg msg = new ResponseMsg();
        Map<String, Object> allCustomerByDataMap = customerMapper.getAllCustomerByData();
        if (allCustomerByDataMap.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("未查询到相关信息");
            return msg;
        }
        msg.setCode("200");
        msg.setData(allCustomerByDataMap);
        return msg;
    }


}
